package com.example.administrator.recyclerviewtest319hz.model;

import com.example.administrator.recyclerviewtest319hz.bean.ArticleBean;
import com.example.administrator.recyclerviewtest319hz.iface.ArticleListener;
import com.example.administrator.recyclerviewtest319hz.iface.Articleiface;
import com.example.administrator.recyclerviewtest319hz.service.ArticleService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/4/20.
 */
//获取文章数据ti
public class Articlemodel implements Articleiface{
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public Articlemodel(){
        retrofit=new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();

    }
    //获取文章数据
    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener articleListener) {
        ArticleService articleService = retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call = articleService.getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful()&&response!=null){

                    articleListener.onReaponse(response.body());
                }else
                    articleListener.onFail("onResponse fail");
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                    articleListener.onFail(t.toString());
            }
        });
    }
}
